﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matrix.MaKaYunShang.WeChatUserInfoSync
{
    public partial class SyncUserForm : Form
    {
        public SyncUserThread SyncUserInfoThread;

        public SyncUserForm()
        {
            InitializeComponent();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtAppId.Text))
            {
                return;
            };

            SyncUserInfoThread = new SyncUserThread(txtAppId.Text);
            SyncUserInfoThread.OnEventSyncUserInfo +=
                CreateThread_OnEventSyncUserInfo;

            SyncUserInfoThread.CreateNewThread();
            SyncUserInfoThread.StartThread();

            btnStart.Enabled = false;
            btnStop.Enabled = true;

        }

        public void CreateThread_OnEventSyncUserInfo(object sender, EventArgs e)
        {
            Invoke((EventHandler)(delegate
            {
                lbDone.Text = ((SyncUserThread) sender).NDoneUnion.ToString();
            }));
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            SyncUserInfoThread.StopThread();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void SyncUserForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (SyncUserInfoThread != null)
            {
                SyncUserInfoThread.StopThread();
            }
            
        }
    }
}
